package com.briup.bms.service.impl;

import java.util.*;

import com.briup.bms.common.exceptions.BmsServiceException;
import com.briup.bms.common.utils.ResultCode;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.bms.bean.Employee;
import com.briup.bms.common.utils.EmpInfoHolder;
import com.briup.bms.dao.IEmpMapper;
import com.briup.bms.service.IEmpService;
import com.github.pagehelper.PageInfo;

@Service
public class EmpServiceImpl implements IEmpService {
	
	@Autowired
	private IEmpMapper empMapper;

	@Override
	public PageInfo<Employee> findByConditon(int pageNum, int pageSize, 
			String username, Integer deptId, Date startDate,Date endDate,String title) {
		// TODO Auto-generated method stub
		/*
		 *  1. 获取当前登陆的用户信息。
		 *  2.判断用户角色是否为President
		 *  	2.1 如果是，查询所有员工信息
		 *  	2.2 如果不是，查询该员工所管辖的所有员工的信息。
		 */
		Employee emp = EmpInfoHolder.getCurrentUser();
		username = (username==null||"".equals(username))?username:"%"+username+"%";
		PageHelper.startPage(pageNum,pageSize);
		List<Employee> employeeList=null;
		if("President".equals(emp.getTitle())) { //总裁们可以查看所有
			employeeList=empMapper.findAll(null,username, deptId, startDate, endDate, title);
			return new PageInfo<>(employeeList);
		}else {
			//如果职称不是总裁，需要查询作为经理所管理的所有员工，以及间接管理的员工的信息。
			Integer mid=empMapper.findMidByName(username);
			// 保存所有管理id
			List<Integer> managerId=empMapper.findIdsByMids(Collections.singletonList(mid));
			//保存每次查询得到得管理id
			List<Integer> mids=managerId;
			while (!managerId.isEmpty()){
				List<Integer> midss=empMapper.findIdsByMids(mids);
				if (midss.isEmpty()){
					break;
				}else {
					//将本次查到得id（下属id）加入查询需要得id列表
					managerId.addAll(midss);
					mids=midss;
				}
			}
		managerId.add(mid);
		employeeList=empMapper.findEmpByManager(managerId,username,deptId,startDate,endDate,title);
		}
		return new PageInfo<>(employeeList);
	}

	@Override
	public PageInfo<Employee> findByPageHelper(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum,pageSize);
		List<Employee> employeeList =empMapper.findAll(null,null,null,null,null,null);
		return new PageInfo<>(employeeList);
	}

	@Override
	public void saveOrUpdateEmployee(Employee employee) {
		if (employee.getId()==null) {

			int rows = empMapper.insertEmployee(employee);
			if (rows <= 0) {
				throw new BmsServiceException(ResultCode.OPERATE_FAILED_INSERT, "数据插入失败，请重新尝试!");
			}
		}else {
			this.updateEmployee(employee);
		}
	}

	@Override
	public void updateEmployee(Employee employee) {
		Employee oldEmployee=empMapper.findUserId(employee.getUserId());
		if (ObjectUtils.isNotEmpty(oldEmployee)){
			int rows=empMapper.updateEmployee(employee);
			if (rows<=0){
				throw new BmsServiceException(ResultCode.OPERATE_FAILED_UPDATE,"数据更新失败，请重新尝试!");
			}
		}else {
			throw new BmsServiceException(ResultCode.OPERATE_FAILED_UPDATE,"更新的数据不存在!");
		}
	}

	@Override
	public Map<String, List<?>> statisticEmpByRegion() {
		List<Map<String, Object>> result = empMapper.countEmpByRegion();
		List<String> labels = new ArrayList<>();
		List<Integer> data = new ArrayList<>();
		for (Map<String, Object> map : result) {
			labels.add(map.get("name").toString());
			data.add(Integer.parseInt(map.get("num").toString()));
		}
		Map<String, List<?>> map = new HashMap<>();
		map.put("labels", labels);
		map.put("data", data);
		return map;
	}

	@Override
	public List<Map<String, Object>> statisticEmpByRegion2() {
		// TODO Auto-generated method stub
		return empMapper.countEmpByRegion();
	}

	@Override
	public void deleteEmp(String empId) {
		empMapper.deleteById(empId);
	}

	@Override
	public void batchDeleteEmp(Set<Integer> empIds) {
		empMapper.batchDeleteEmp(empIds);
	}

}
